import 'dart:convert';
import 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:flutter_utils/flutter_utils.dart';

Future<String?> calculateMD5WithCrypto(String filePath) async {
  var ret = '';
  var file = File(filePath);
  if (await file.exists()) {
    try {
      var hash = await md5.bind(file.openRead()).first;
      ret = base64.encode(hash.bytes);
    } catch (exception) {
      LogUtil.logD('Unable to evaluate the MD5 sum :$exception');
      return null;
    }
  } else {
    LogUtil.logD(
        '`$filePath` does not exits so unable to evaluate its MD5 sum.');
    return null;
  }
  return ret;
}
